﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    public partial class TVR05
    {
        #region ==IsFilterByVKP===========
        public static readonly DependencyProperty IsFilterByVKPProperty = DependencyProperty.Register("IsFilterByVKP",
            typeof(bool), typeof(TVR05),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsFilterByVKPChanged));
        public bool IsFilterByVKP
        {
            get
            {
                return (bool)GetValue(IsFilterByVKPProperty);
            }
            set
            {
                SetValue(IsFilterByVKPProperty, value);
            }
        }
        private static void OnIsFilterByVKPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TVR05;
            if (src != null)
            {
                src.IsFilterByVKP = (bool)e.NewValue;
            }
        }
        #endregion


        #region ==dwVKP===========
        public static readonly DependencyProperty dwVKPProperty = DependencyProperty.Register("dwVKP",
            typeof(DataRow), typeof(TVR05),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwVKPChanged));
        public DataRow dwVKP
        {
            get
            {
                return (DataRow)GetValue(dwVKPProperty);
            }
            set
            {
                SetValue(dwVKPProperty, value);

            }
        }
        private static void OndwVKPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TVR05;
            DataRow row = (DataRow)e.NewValue;
            if (src != null)
            {
                src.dwVKP = row;
                src.LOADBYVKP();
            }
        }
        #endregion

        #region ==dwZB===========
        public static readonly DependencyProperty dwZBProperty = DependencyProperty.Register("dwZB",
            typeof(DataRow), typeof(TVR05),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwZBChanged));
        public DataRow dwZB
        {
            get
            {
                return (DataRow)GetValue(dwZBProperty);
            }
            set
            {
                SetValue(dwZBProperty, value);
            }
        }
        private static void OndwZBChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TVR05;
            DataRow row = (DataRow)e.NewValue;
            if (src != null)
            {
                src.dwZB = row;
            }
        }
        #endregion

        public TVR05()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVRPOS();
            DataContext = MV;
            InitializeComponent();
        }

        public override void LOADBYRENR(string value)
        {
            base.LOADBYRENR(value);
        }
        public override void OnLoadChg(bool value)
        {
            base.OnLoadChg(value);
            if (!IsCanLoad)
                return;
            dwData = dwVKP;
            dwVKP = null;
            dwVKP = dwData;
            string key = RENR;
            RENR = "";
            RENR = key;
        }
        public void LOADBYVKP()
        {
            if (!IsCanLoad)
                return;
            TBMain.Clear();
            if (dwVKP == null)
                return;
            if (dwZB == null)
                return;
            VKP = "K" + dwVKP["VKPAUFKRS"].ToString() + dwVKP["VKPAUFLFD"].ToString() + dwVKP["VKPAUFPOS"].ToString();
            if (!IsAuto)
                return;

            filter = " VRPRENR='" + RENR + "' AND VRPAUFKRS='" + VKP.Substring(1, 2) + "' AND VRPAUFLFD='" + VKP.Substring(3, 6) + "' AND VRPAUFPOS='" + VKP.Substring(9, 3) + "'";
            sort = " VRPREPOS ASC";
            rows = TBMain.Select(filter, sort);
            if (rows.Length == 0)
            {
                TMV.NEWBYVKP(dwVKP, dwZB, RENR);
            }
            if (IsFilterByVKP)
            {
                cvMaster.CustomFilter = filter;
            }
            vwMaster.View.MoveCurrentToFirst();
        }

        public override void LOADBYVKP(string vkp)
        {
            if (vkp.Length < 11) return;

            filter = " VRPRENR='" + RENR + "' AND VRPAUFKRS='" + vkp.Substring(1, 2) + "' AND VRPAUFLFD='" + vkp.Substring(3, 6) + "' AND VRPAUFPOS='" + vkp.Substring(9, 3) + "'";
            sort = " VRPREPOS ASC";
            rows = TBMain.Select(filter, sort);
            if (rows.Length == 0)
            {
                base.LOADBYVKP(vkp);
            }
            if (IsFilterByVKP)
            {
                cvMaster.CustomFilter = filter;
            }
            vwMaster.View.MoveCurrentToFirst();
        }

        private void SplitCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsNew)
                {
                    IsNew = true;
                    MessageBox.Show("请输入拆分比例");
                    return;
                }
                if (SQTY <= 0)
                    return;
                if (dwMaster == null)
                    return;
                dwData = TBMain.NewRow();
                dwData.ItemArray = dwMaster.ItemArray;
                ((VVRPOS)MV).GetPOS(dwData, 9);
                double qty = System.Convert.ToDouble(dwMaster["VRPGESWERT"].ToString());
                double sqty = Helper.RoundX(qty * SQTY, 1);
                dwMaster["VRPGESWERT"] = sqty;
                dwData["VRPGESWERT"] = Helper.RoundX(qty - sqty, 1);
                for (int i = 1; i < 6; i++)
                {
                    dwData["VRPRABART" + i] = "   ";
                    dwData["VRPRABPROZ" + i] = "0";
                    dwData["VRPRABWERT" + i] = 0;
                    dwData["VRPZUART" + i] = "   ";
                    dwData["VRPZUABPROZ" + i] = "0";
                    dwData["VRPZUABWERT" + i] = 0;
                }
                TMV.Insert(dwData);
                TMV.UpdateVRPOS(dwMaster, 0);
                SQTY = -1;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void CopyCommand(object sender, RoutedEventArgs e)
        {

            base.CopyCommand(sender, e);
        }



        private void ShowAllCommand(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = null;
            IsFilterByVKP = !IsFilterByVKP;
            if (IsFilterByVKP)
            {
                cShow.Content = "按销售单";
            }
            else
            {
                cShow.Content = "全部";
            }
        }

        public override void LOADBYAUF()
        {
            Locate("KEY", ORDERNUM);
        }
        /// <summary>
        /// 待处理现金不断变化
        /// 所有单行满足要求的都会被重新计算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        DataRow dwPay = null;

        private void ConfirmVK04PaymentCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                dwPay = null;
                if (dwMaster == null)
                {
                    MessageBox.Show("请选择一行发票");
                    return;
                }


                filter = "VRPAUFKRS='" + dwMaster["VRPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwMaster["VRPAUFLFD"].ToString()
                    + "' AND VRPAUFPOS='" + dwMaster["VRPAUFPOS"].ToString() + "' AND VRPZOLLTNR LIKE '1:%'";
                rows = TBMain.Select(filter);
                foreach (DataRow row in rows)
                {
                    dwPay = row;
                }
                if (dwPay == null)
                    throw new Exception("没有需要支付的订金");
                IsVKPay = true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void ConfirmVVPaymentCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                dwPay = null;
                if (dwMaster == null)
                {
                    throw new Exception("请选择一行发票");
                }

                filter = "VRPAUFKRS='" + dwMaster["VRPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwMaster["VRPAUFLFD"].ToString()
                  + "' AND VRPAUFPOS='" + dwMaster["VRPAUFPOS"].ToString() + "' AND VRPZOLLTNR LIKE '2:%'";
                rows = TBMain.Select(filter);
                foreach (DataRow row in rows)
                {
                    dwPay = row;
                }
                if (dwPay == null)
                    throw new Exception("没有需要支付的提货款");
                IsVVPay = true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void ConfirmRESTPaymentCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                dwPay = null;
                if (dwMaster == null)
                {
                    throw new Exception("请选择一行发票");
                }
                filter = "VRPAUFKRS='" + dwMaster["VRPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwMaster["VRPAUFLFD"].ToString()
                  + "' AND VRPAUFPOS='" + dwMaster["VRPAUFPOS"].ToString() + "' AND VRPZOLLTNR LIKE '3:%'";
                rows = TBMain.Select(filter);
                foreach (DataRow row in rows)
                {
                    dwPay = row;
                }
                if (dwPay == null)
                    throw new Exception("没有需要支付的尾款");
                IsRest = true;
                //判断销售单状态
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        public override void OnPAIDVALUEChg(string value)
        {
            try
            {


                if (dwPay == null)
                    return;
                if (value == "")
                    return;
                string paytype = value.Substring(0, 10).Trim();
                double v = double.Parse(value.Substring(10, 20).Trim());
                switch (paytype)
                {
                    case "LINEPAY":
                        SVALUE = v;
                        LinePaid(dwPay, false, false, false);
                        break;
                    #region ===VK04PAY======

                    case "VK04PAY":
                        try
                        {
                            if (dwPay == null)
                                throw new Exception("没有需要支付的订金");
                            SVALUE = v;
                            filter = "VRPAUFKRS='" + dwPay["VRPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwPay["VRPAUFLFD"].ToString() + "' AND VRPZOLLTNR LIKE '1:%'";
                            sort = " VRPAUFPOS ASC";
                            rows = TBMain.Select(filter);
                            foreach (DataRow dwLine in rows)
                            {
                                LinePaid(dwLine, true, false, false);// 
                            }
                            IsVKPay = false;
                        }
                        catch (Exception ex)
                        {
                            Tracer.Error(ex.StackTrace, ex);
                        }
                        break;
                    #endregion
                    #region ===VVPAY======
                    case "VVPAY":
                        try
                        {
                            if (dwPay == null)
                                throw new Exception("没有需要支付的提货款");
                            SVALUE = v;
                            filter = "VRPAUFKRS='" + dwPay["VRPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwPay["VRPAUFLFD"].ToString() + "' AND VRPZOLLTNR LIKE '2:%'";
                            sort = " VRPAUFPOS ASC";
                            rows = TBMain.Select(filter);
                            foreach (DataRow dwLine in rows)
                            {
                                LinePaid(dwLine, false, true, false);//
                            }
                            IsVVPay = false;
                        }
                        catch (Exception ex)
                        {
                            Tracer.Error(ex.StackTrace, ex);
                        }
                        break;
                    #endregion
                    #region ===RESTPAY======

                    case "RESTPAY":
                        IsRest = true;
                        try
                        {
                            if (dwPay == null)
                                throw new Exception("没有需要支付的尾款");
                            SVALUE = v;
                            filter = "VRPAUFKRS='" + dwPay["VRPAUFKRS"].ToString() + "' AND VRPAUFLFD='" + dwPay["VRPAUFLFD"].ToString() + "' AND VRPZOLLTNR LIKE '3:%'";
                            sort = " VRPAUFPOS ASC";
                            rows = TBMain.Select(filter);
                            foreach (DataRow dwLine in rows)
                            {
                                LinePaid(dwLine, false, false, true);// 
                            }
                            IsRest = false;
                            //判断销售单状态
                        }
                        catch (Exception ex)
                        {
                            Tracer.Error(ex.StackTrace, ex);
                        }
                        break;
                    #endregion
                    default:
                        SVALUE = v;
                        if (dwPay == null)
                        {
                            MessageBox.Show("请选择一行发票");
                            return;
                        }
                        ManulPaid(dwPay, true, false, false);//   
                        IsMPay = false;
                        break;
                }
                base.OnPAIDVALUEChg(value);
                SVALUE = 0;
                IsNew = false;
                IsRest = false;
                IsVKPay = false;
                IsVVPay = false;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }

        private void ManualPayment(object sender, RoutedEventArgs e)
        {

            if (dwMaster == null)
            {
                MessageBox.Show("请选择一行发票");
                return;
            }

            if (dwMaster == null)
                return;
            if (!IsMPay)
            {
                SVALUE = 0;
                IsMPay = true;
                return;
            }

            ManulPaid(dwMaster, true, false, false);//   
            IsMPay = false;
        }
        public override void OnEXPAYChg(double _EXPAY)
        {
            if (!IsMPay)
                return;
            SVALUE = _EXPAY;
            if (dwMaster == null)
            {
                MessageBox.Show("请选择一行发票");
                return;
            }
            ManulPaid(dwMaster, true, false, false);//   
            IsMPay = false;
            base.OnEXPAYChg(_EXPAY);
        }

        public override void OnPaidChg(bool paied)
        {
            if (paied)
            {
                cvMaster.CustomFilter = "VRPERLKZ='J'";
            }
            else
            {
                cvMaster.CustomFilter = null;
            }
            base.OnPaidChg(paied);
        }
    }
}